package com.sws.reservation.service.impl;

import java.util.List;
import com.sws.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sws.reservation.mapper.RyReservationMapper;
import com.sws.reservation.domain.RyReservation;
import com.sws.reservation.service.IRyReservationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 用户预约服务Service业务层处理
 * 
 * @author cx
 * @date 2025-06-30
 */
@Service
public class RyReservationServiceImpl implements IRyReservationService 
{
    private static final Logger logger = LoggerFactory.getLogger(RyReservationServiceImpl.class);
    
    @Autowired
    private RyReservationMapper ryReservationMapper;

    /**
     * 查询用户预约服务
     * 
     * @param id 用户预约服务主键
     * @return 用户预约服务
     */
    @Override
    public RyReservation selectRyReservationById(Long id)
    {
        return ryReservationMapper.selectRyReservationById(id);
    }

    /**
     * 查询用户预约服务列表
     * 
     * @param ryReservation 用户预约服务
     * @return 用户预约服务
     */
    @Override
    public List<RyReservation> selectRyReservationList(RyReservation ryReservation)
    {
        return ryReservationMapper.selectRyReservationList(ryReservation);
    }

    /**
     * 新增用户预约服务
     * 
     * @param ryReservation 用户预约服务
     * @return 结果
     */
    @Override
    public int insertRyReservation(RyReservation ryReservation)
    {
        ryReservation.setCreateTime(DateUtils.getNowDate());
        return ryReservationMapper.insertRyReservation(ryReservation);
    }

    /**
     * 修改用户预约服务
     * 
     * @param ryReservation 用户预约服务
     * @return 结果
     */
    @Override
    public int updateRyReservation(RyReservation ryReservation)
    {
        ryReservation.setUpdateTime(DateUtils.getNowDate());
        return ryReservationMapper.updateRyReservation(ryReservation);
    }

    /**
     * 批量删除用户预约服务
     * 
     * @param ids 需要删除的用户预约服务主键
     * @return 结果
     */
    @Override
    public int deleteRyReservationByIds(Long[] ids)
    {
        return ryReservationMapper.deleteRyReservationByIds(ids);
    }

    /**
     * 删除用户预约服务信息
     * 
     * @param id 用户预约服务主键
     * @return 结果
     */
    @Override
    public int deleteRyReservationById(Long id)
    {
        return ryReservationMapper.deleteRyReservationById(id);
    }
    
    /**
     * 根据技师PID查询相关预约
     * 
     * @param staffPid 技师PID
     * @param status 预约状态(可选)
     * @return 预约列表
     */
    @Override
    public List<RyReservation> selectRyReservationByStaffPid(String staffPid, Integer status)
    {
        RyReservation queryParams = new RyReservation();
        queryParams.setStaffPid(staffPid);
        if (status != null) {
            queryParams.setStatus(status);
        }
        return ryReservationMapper.selectRyReservationList(queryParams);
    }
    
    /**
     * 根据顾客PID查询相关预约
     * 
     * @param customerPid 顾客PID
     * @param status 预约状态(可选)
     * @return 预约列表
     */
    @Override
    public List<RyReservation> selectRyReservationByCustomerPid(String customerPid, Integer status)
    {
        RyReservation queryParams = new RyReservation();
        queryParams.setCustomerPid(customerPid);
        if (status != null) {
            queryParams.setStatus(status);
        }
        return ryReservationMapper.selectRyReservationList(queryParams);
    }
    
    /**
     * 取消预约
     * 
     * @param id 预约ID
     * @param customerPid 顾客PID(用于验证)
     * @return 结果
     */
    @Override
    public int cancelReservation(Long id, String customerPid)
    {
        // 先查询预约是否存在，且属于该顾客
        RyReservation reservation = selectRyReservationById(id);
        if (reservation == null) {
            logger.warn("预约不存在，无法取消: id={}", id);
            return 0;
        }
        
        if (!customerPid.equals(reservation.getCustomerPid())) {
            logger.warn("预约不属于该顾客，无权取消: id={}, requestCustomerPid={}, actualCustomerPid={}", 
                id, customerPid, reservation.getCustomerPid());
            return 0;
        }
        
        // 只有待确认和已确认的预约才能取消
        if (reservation.getStatus() != 1 && reservation.getStatus() != 2) {
            logger.warn("预约状态不允许取消: id={}, status={}", id, reservation.getStatus());
            return 0;
        }
        
        // 更新状态为已取消
        reservation.setStatus(4); // 已取消
        reservation.setUpdateTime(DateUtils.getNowDate());
        return ryReservationMapper.updateRyReservation(reservation);
    }
    
    /**
     * 更新预约状态
     * 
     * @param id 预约ID
     * @param status 新状态
     * @param staffPid 技师PID(用于验证)
     * @return 结果
     */
    @Override
    public int updateReservationStatus(Long id, Integer status, String staffPid)
    {
        // 先查询预约是否存在，且由该技师负责
        RyReservation reservation = selectRyReservationById(id);
        if (reservation == null) {
            logger.warn("预约不存在，无法更新状态: id={}", id);
            return 0;
        }
        
        if (reservation.getStaffPid() == null || !staffPid.equals(reservation.getStaffPid())) {
            logger.warn("预约不属于该技师，无权更新状态: id={}, requestStaffPid={}, actualStaffPid={}", 
                id, staffPid, reservation.getStaffPid());
            return 0;
        }
        
        // 状态流转检查
        if (!isValidStatusTransition(reservation.getStatus(), status)) {
            logger.warn("预约状态流转无效: id={}, currentStatus={}, newStatus={}", 
                id, reservation.getStatus(), status);
            return 0;
        }
        
        // 更新状态
        reservation.setStatus(status);
        reservation.setUpdateTime(DateUtils.getNowDate());
        return ryReservationMapper.updateRyReservation(reservation);
    }
    
    /**
     * 检查状态流转是否有效
     * 
     * @param currentStatus 当前状态
     * @param newStatus 新状态
     * @return 是否有效
     */
    private boolean isValidStatusTransition(Integer currentStatus, Integer newStatus) {
        if (currentStatus == null || newStatus == null) {
            return false;
        }
        
        // 状态流转规则：
        // 1:待确认 -> 2:已确认 或 4:已取消
        // 2:已确认 -> 3:已完成 或 4:已取消
        // 其他情况不允许流转
        
        if (currentStatus == 1) {
            return newStatus == 2 || newStatus == 4;
        } else if (currentStatus == 2) {
            return newStatus == 3 || newStatus == 4;
        }
        
        return false;
    }
}
